package com.whh.dota2helper;

import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;
import android.view.LayoutInflater;
import android.view.View;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.TextView;

import com.whh.dota2helper.model.Ability;
import com.whh.dota2helper.service.AbilityService;

/**
 * 英雄故事窗口
 * 
 * @author Splendor
 * 
 */
public class HeroAbilityActivity extends Activity {

	private AbilityService abilityService ;

	private ImageView iv_ability;
	private TextView tv_ability_name;
	private TextView tv_ability_desc;
	private TextView tv_manacost;
	private TextView tv_cooldown;
	private LinearLayout ll_effects;
	private LinearLayout ll_tips;

	private Ability ability;

	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.activity_hero_ability);
		abilityService = new AbilityService(HeroAbilityActivity.this);
		init();
	}

	private void init() {
		Intent intent = this.getIntent();
		long id = intent.getLongExtra("id", 1);
		ability = abilityService.getAbility(id);

		iv_ability = (ImageView) this.findViewById(R.id.iv_ability);
		tv_ability_name = (TextView) this.findViewById(R.id.tv_ability_name);
		tv_ability_desc = (TextView) this.findViewById(R.id.tv_ability_desc);
		tv_manacost = (TextView) this.findViewById(R.id.tv_manacost);
		tv_cooldown = (TextView) this.findViewById(R.id.tv_cooldown);
		ll_effects = (LinearLayout)this.findViewById(R.id.ll_effects);
		ll_tips = (LinearLayout)this.findViewById(R.id.ll_tips);

		tv_ability_name.setText(ability.getName());
		tv_ability_desc.setText(ability.getDesc());
		tv_manacost.setText(ability.getManaCost());
		tv_cooldown.setText(ability.getCoolDown());
		int imgId = this.getResources().getIdentifier(ability.getImg(),"drawable", getPackageName());
		iv_ability.setImageResource(imgId);
		
		if(ability.getEffect()!=null && !ability.getEffect().equals("")){
			String[] effects = ability.getEffect().split("\\|\\|");
			for(int i=0;i<effects.length;i++){
				LinearLayout.LayoutParams lp = new LinearLayout.LayoutParams(LinearLayout.LayoutParams.FILL_PARENT, LinearLayout.LayoutParams.WRAP_CONTENT);  
				lp.setMargins(0, 10, 0, 0);  
				View view = LayoutInflater.from(getApplicationContext()).inflate(R.layout.list_item_ability_effect, null);
				view.setLayoutParams(lp); 
				if(effects[i].split("：").length>=2){
					String key = effects[i].split("：")[0].trim();
					String value = effects[i].split("：")[1].trim();
					TextView tv_effect_key = (TextView)view.findViewById(R.id.tv_effect_key);
					TextView tv_effect_value = (TextView)view.findViewById(R.id.tv_effect_value);
					tv_effect_key.setText(key+"：");
					if(key.startsWith("SCEPTER")){
						tv_effect_key.setTextColor(getResources().getColor(R.color.color_6fe871));
					}
					tv_effect_value.setText(value);
					ll_effects.addView(view);
				}
			}
		}
		
		if(ability.getTips()!=null && !ability.getTips().equals("")){
			String[] tips = ability.getTips().split("\\|\\|");
			for(int i=0;i<tips.length;i++){
				LinearLayout.LayoutParams lp = new LinearLayout.LayoutParams(LinearLayout.LayoutParams.FILL_PARENT, LinearLayout.LayoutParams.WRAP_CONTENT);  
				lp.setMargins(0, 10, 0, 0);  
				View view = LayoutInflater.from(getApplicationContext()).inflate(R.layout.list_item_ability_effect, null);
				view.setLayoutParams(lp); 
				String key = "";
				String value = "";
				if(tips[i].split("：").length>=2){
					key = tips[i].split("：")[0].trim();
					value = tips[i].split("：")[1].trim();
				}else{
					key=tips[i].split("：")[0].trim();
					value="";
				}
				
				TextView tv_effect_key = (TextView)view.findViewById(R.id.tv_effect_key);
				TextView tv_effect_value = (TextView)view.findViewById(R.id.tv_effect_value);
				tv_effect_key.setText(key+(value.equals("")?"":"："));
				if(key.startsWith("神杖升级")){
					tv_effect_key.setTextColor(getResources().getColor(R.color.color_6fe871));
				}
				tv_effect_value.setText(value);
				ll_tips.addView(view);
			}
		}
	}
}
